package com.leather.quan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leather.common.entity.QuanComment;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.CheckKit;
import com.leather.quan.config.MQConfig;
import com.leather.quan.mapper.QuanCommentMapper;
import com.leather.quan.mapper.QuanMapper;
import com.leather.quan.service.CommentService;
import com.leather.quan.vo.CommentAddVo;
import com.leather.quan.vo.CommentQueryVo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<QuanCommentMapper, QuanComment> implements CommentService {


    @Autowired
    private QuanMapper quanMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private QuanCommentMapper quanCommentMapper;


    @Override
    public void deleteAllComment(int quanId) {
        QueryWrapper<QuanComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("quan_id", quanId);
        remove(queryWrapper);
    }

    @Override
    public Result deleteByUserId(int id, int userId) {
        QueryWrapper<QuanComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id).eq("user_id", userId);
        CheckKit.check(remove(queryWrapper), CodeMsg.SERVER_ERROR);
        return Result.success("OK");
    }

    @Override
    public Result addComment(CommentAddVo commentAddVo) {
        Result result = commentAddVo.validate();
        if (!result.isSuccess()) {
            return result;
        }
        QuanComment quanComment = (QuanComment) result.getData();
        amqpTemplate.convertAndSend(MQConfig.QUAN_COMMENTS,quanComment.getQuanId());
        CheckKit.check(save(quanComment), CodeMsg.SERVER_ERROR);
        return result;
    }

    @Override
    public Result queryCommentByQuanId(int quanId) {
        CheckKit.check(quanId>0,CodeMsg.ERROR_QUAN_ID);
        List<CommentQueryVo> commentQueryVos = quanCommentMapper.queryCommentByQuanId(quanId);
        return Result.success(commentQueryVos);
    }
}
